import java.util.Arrays;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: lv
 * Date: 2023-03-18
 * Time: 12:04
 */

class Node{
    public int val;
    public Node left;
    public Node right;

    public Node(int val){
        this.val=val;
    }
}
public class BinarySearchTree {

    public Node root = null;

    public Node search(int key){

        if(root==null){
            return null;
        }
        Node cur=root;
        while(cur!=null){
            if(cur.val<key){
                cur=cur.right;
            } else if (cur.val==key) {
                return root;
            }else{
                cur=cur.left;
            }
        }
        return null;
    }

    public boolean insert(int val){
        if(root==null){
            root=new Node(val);
            return true;
        }
        Node parent=null;
        Node cur=root;
        while(cur!=null){
            if(cur.val>val){
                parent=cur;
                cur=cur.left;
            }else if(cur.val==val){
                return false;
            }else{
                parent=cur;
                cur=cur.right;
            }
        }
        if(parent.val>val){
            parent.left=new Node(val);
        }else{
            parent.right=new Node(val);
        }
        return true;
    }


    public void remove(int key){
        if(root==null){
            return ;
        }
        Node cur=root;//cur要删除的节点
        Node parent=null;
        while(cur!=null){
            if(cur.val==key){
                removeNode(cur,parent);
                break;
            } else if (cur.val<key) {
                parent=cur;
                cur=cur.right;
            }else{
                parent=cur;
                cur=cur.left;
            }
        }

    }
    public void removeNode(Node cur,Node parent){
        if(cur.left==null){
            if(cur==root){
                root=cur.right;
            } else if (cur==parent.left) {
                parent.left=cur.right;
            }else{
                parent.right=cur.right;
            }
        }else if(cur.right==null){
            if(cur==root){
                root=cur.left;
            } else if (cur==parent.left) {
                parent.left=cur.left;
            }else{
                parent.right=cur.left;
            }
        }else{
            Node targetParent=cur;
            Node target=cur.right;
            while(target.left!=null){
                targetParent=target;
                target=target.left;
            }
            cur.val=target.val;
            if(target==targetParent.left){
                targetParent.left=target.right;
            }else{
                targetParent.right=target.right;
            }

        }

    }


    public void inOrder(Node root){
        if(root==null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    public static void main(String[] args) {
        int[] array={10,8,16,9,34,56,32,21,88};
        BinarySearchTree binarySearchTree=new BinarySearchTree();
        for (int i = 0; i < array.length; i++) {
            binarySearchTree.insert(array[i]);
        }
        binarySearchTree.inOrder(binarySearchTree.root);
        System.out.println();
        System.out.println("插入重复的数据：");
        System.out.println(binarySearchTree.insert(16));

        System.out.println("删除数据");
        binarySearchTree.remove(10);
        binarySearchTree.inOrder(binarySearchTree.root);
        System.out.println();

    }

}
